<?php

include_once 'accessMysql.php';
include_once 'baseInformatica.php';
include_once 'getAccessToken.php';
include_once 'headimgTrans.php';
include_once 'getJson.php';

class wechatCallback {

    public $fromUsername = '';
    public $toUsername = '';
    private $openid;

    public function valid() {
        $echoStr = $_GET["echostr"];
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature() {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

    public function responseMsg() {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $fromUsername = $postObj->FromUserName;

        $this->openid = $postObj->FromUserName;

        $toUsername = $postObj->ToUserName;
        $type = $postObj->MsgType;
        $event = $postObj->Event;
        $Event_Key = $postObj->EventKey;
        $mid = $postObj->MediaId;
        $link = $postObj->Url;

        $latitude = $postObj->Location_X;
        $longitude = $postObj->Location_Y;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[text]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
        if ($keyword != '') {
            if ($keyword == '我的头像')
                goto touxiang;
            $contentStr = $this->resWordre($keyword);
        }  elseif ($type == "image") {
            $contentStr = "您发送的是图片消息,消息的MediaId是" . $mid;
        } elseif ($type == "voice") {
            $contentStr = "您发送的是语音消息,消息的MediaId是" . $mid;
        } elseif ($type == "video") {
            $contentStr = "您发送的是视频消息,消息的MediaId是" . $mid;
        } elseif ($type == "location") {
            $contentStr = "您发送的是地理位置消息,您的地理位置是：经度" . $latitude . "维度：" . $longitude;
        } elseif ($type == "link") {
            $contentStr = "您发送的是链接消息,消息链接为" . $link;
        } elseif ($type == "event" && $event == "subscribe") {
            $contentStr = "欢迎关注isecs!!!\n提示：回复我的信息获取您的信息，回复我的头像获取您的头像。";
        } elseif ($type == "event" && $event == "unsubscribe") {
            $contentStr = "期待您再次关 注!!!";
        } elseif ($type == "event") {
            //$contentStr = "其他事件";
        } else {
            //$contentStr = "未知操作";
        }
      if ($keyword == '客服') {
//        $contentStr = $this->transmitService($object);
//        return $contentStr;
          $textTpl = "<xml>
        <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[transfer_customer_service]]></MsgType>
       </xml>
       ";
    $response = sprintf($textTpl, $fromUsername, $toUsername, time());
   echo $response;
      }
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
        echo $resultStr;
        return;

        touxiang://回复头像
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
        $result = sprintf($textTpl, $fromUsername, $toUsername, $time, transId($this->g_address()));
        echo $result;
    }

    private function resWordre($word) {
        if ($word == "我的信息") {
            $response = $this->userinformation();
        } elseif ($word == "头像id" || $word == "头像ID") {
            $response = transId($this->g_address());
        } else {
            $response = "您发送的是文本消息，内容是：\n$word";
        }
        return $response;
    }

    private function userinformation() {

        $access_token = GetAccessToken();

        $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$this->openid&lang=zh_CN";

        $userinfo = getJson($get_user_info_url);
        print_r($userinfo);

        $nicheng = $userinfo['nickname'];
        if ($userinfo['sex'] = 1) {
            $xingbie = '男';
        } elseif ($userinfo['sex'] = 2) {
            $xingbie = '女';
        } else {
            $xingbie = '未知';
        }

        $lauguage = $userinfo['language'];
        $openid = $userinfo['openid'];
        $city = $userinfo['city'];
        $province = $userinfo['province'];
        $country = $userinfo['country'];
        $headimgurl = $userinfo['headimgurl'];
        $subscribe = date("Y-m-d H:i:s", $userinfo['subscribe_time']);
        $groupid = $userinfo['groupid'];

        $response = "您的信息如下：\n昵称：$nicheng\n"
                . "性别：$xingbie\n语言：$lauguage\n"
                . "所在地：$country  $province  $city\n"
                . "头像链接：$headimgurl\n" . "OpenId：$openid\n"
                . "关注时间：$subscribe\ngroupid：$groupid";
        return $response;
    }

    private function g_address() {

        $access_token = GetAccessToken();

        $get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$this->openid&lang=zh_CN";
        $userinfo = getJson($get_user_info_url);

        $headimgurl = $userinfo['headimgurl'];
        return $headimgurl;
    }


//  private  function transmitService($object) {
//    $textTpl = "<xml>
//        <ToUserName><![CDATA[touser]]></ToUserName>
//        <FromUserName><![CDATA[fromuser]]></FromUserName>
//        <CreateTime>1399197672</CreateTime>
//        <MsgType><![CDATA[transfer_customer_service]]></MsgType>
//        <TransInfo>
//       <KfAccount><![CDATA[test1@test]]></KfAccount>
//        </TransInfo>
// </xml>";
//    $response = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
//   echo $response;

}
